package com.dingdang.controller.admin.admin;

import com.dingdang.model.Admin;
import com.dingdang.model.AdminLeftNav;
import com.dingdang.service.admin.AdminService;
import com.dingdang.util.LogConfig;
import com.dingdang.util.RedisUtil;
import com.dingdang.util.RestControllerHelper;
import com.dingdang.util.VerificationCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 后台纯接口数据返回数据控制类
 */
@Controller
public class AdminController {

    @Autowired
    private AdminService adminService;

    private static int ExpireTime = 60;   // redis中存储的过期时间60s


    /**
     * 后台登陆用户获取验证码
     */
    @ResponseBody
    @RequestMapping(value = "/getCodeUser", produces = MediaType.IMAGE_JPEG_VALUE)
    public void getCodeUser(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setContentType("image/jpeg");//设置相应类型,告诉浏览器输出的内容为图片
            response.setHeader("Pragma", "No-cache");//设置响应头信息，告诉浏览器不要缓存此内容
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expire", 0);
            VerificationCodeUtil.drawVerificationCode(request, response, 160, 40, 4);//输出验证码图片方法
        } catch (Exception e) {
            LogConfig.error("获取验证码失败>>>> ");
        }
    }

    //    produces = {"application/json;charset=UTF-8"}
    @ResponseBody
    @RequestMapping(value = "/loginAdmin", produces = MediaType.APPLICATION_JSON_VALUE)
    public Map<String, Object> loginUser(Admin admin, String verifyInput, HttpSession session) {
        String uname = admin.getLogin_name();
        String upwd = admin.getPassword();
        String random = (String) session.getAttribute("RANDOMVALIDATECODEKEY");
        Map<String, String> list = new HashMap<>();
        RestControllerHelper helper = new RestControllerHelper();
        if (uname.isEmpty() || upwd.isEmpty() || verifyInput.isEmpty()) {
            helper.setMsg("请输入正确的 用户名，密码，验证码");
            return helper.toJsonMap();
        }
        if (null == random) {
            helper.setMsg("验证码校验失败");
            return helper.toJsonMap();
        }
        try {
            if (random.equals(verifyInput)) {
                int state = adminService.getAdminByUserNameAndPassWord(admin);
                if (state == 1) {
                    helper.setCode(helper.CODE_0);
                    helper.setMsg("登录成功");
                } else {
                    helper.setMsg("账号密码错误");
                }
            } else {
                helper.setMsg("验证码校验失败");
            }
        } catch (Exception e) {
            helper.setMsg("数据异常");
            e.printStackTrace();
        }
        helper.setData(list);
        return helper.toJsonMap();
    }


}
